package cn.kgc.shiro.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.lang.util.ByteSource;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author 课工场
 * @date 2024/8/21
 * @description   用户自定义realm   后续可以在该类中实现基于数据库的认证和授权操作
 */
public class CustomerRealm extends AuthorizingRealm {

    //  授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


    // 认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
        System.out.println("username = " + username);
        // TODO   根据用户名查询数据库    root  123456  第三个参数表示   加密使用的盐salt
        if ("root".equals(username)){
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("root",
                    "47f3bebaa5baf1b61124697c5f1a786a",
                    ByteSource.Util.bytes("java2425"),
                    this.getName());
            return simpleAuthenticationInfo;
        }
        return null;   //  返回null值 则在上层代码中会直接抛出  UnknownAccountException   用户名不存在
    }
}
